package com.hiss.basic.starter.security.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hiss.basic.starter.security.config.properties.SecurityProperties;
import com.hiss.basic.starter.security.handler.RequestUrlHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@EnableConfigurationProperties(SecurityProperties.class)
public class SecurityConfig implements WebMvcConfigurer {

    private final SecurityProperties securityProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handler -> {
            List<String> urls = SpringUtil.getBean(RequestUrlHandler.class).getUrls();
            SaRouter.match(urls).check(StpUtil::checkLogin);
        }).isAnnotation(false)).addPathPatterns("/**").excludePathPatterns(securityProperties.getIgnore());
    }
}
